<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备名称" prop="alias">
      <el-input v-model="dataForm.alias" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备序列号" prop="sn">
      <el-input v-model="dataForm.sn" placeholder="设备序列号"></el-input>
    </el-form-item>
    <el-form-item label="设备型号" prop="devicename">
      <el-input v-model="dataForm.devicename" placeholder="设备型号"></el-input>
    </el-form-item>
    <el-form-item label="设备IP" prop="ipddress">
      <el-input v-model="dataForm.ipddress" placeholder="设备IP"></el-input>
    </el-form-item>
    <el-form-item label="设备类型" prop="productype">
      <el-input v-model="dataForm.productype" placeholder="设备类型"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="0正常其余删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="0正常其余删除"></el-input>
    </el-form-item>
    <el-form-item label="操作人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="操作人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/hikcdcequioment'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          equiomentId: 0,
          alias: '',

          sn: '',

          devicename: '',

          ipddress: '',

          productype: '',

          createTime: '',

          updateTime: '',

          delFlag: '',

          updateBy: '',

        },
        dataRule: {
          alias: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],

          sn: [
            { required: true, message: '设备序列号不能为空', trigger: 'blur' }
          ],

          devicename: [
            { required: true, message: '设备型号不能为空', trigger: 'blur' }
          ],

          ipddress: [
            { required: true, message: '设备IP不能为空', trigger: 'blur' }
          ],

          productype: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],

          delFlag: [
            { required: true, message: '0正常其余删除不能为空', trigger: 'blur' }
          ],

          updateBy: [
            { required: true, message: '操作人不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.equiomentId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.equiomentId) {
            getObj(this.dataForm.equiomentId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.equiomentId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
